﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Reflection;
using System.Threading;
using DaInterFace;

using DaInterFace.Mobile;
using DaInterFace.PDA;
using DaInterFace.SYN;
using DaInterFace.UMode;
using DaFunction;
//using L01;

namespace DataApplication
{
    public partial class MainForm : Form
    {
        private const int MAX_LINE = 100;

        private IDataCenter dataCenter;
        private Boolean allowClose = false;
        private Boolean isAutoMinForm = false;

        #region Form---------------------------------------

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }

        public MainForm()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;

            System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest;

            dataCenter = new DataCenter();
            dataCenter.DaData.MessageEvent += new DataBaseMessage(DataBase_MessageEvent);
            dataCenter.DaMobile.MessageEvent += new MobileMessageHandler(Mobile_MessageEvent);
            dataCenter.DaMobile.ClientMessage.MessageChanged += new MobileClientMessageChange(ClientMessage_MessageChanged);
            dataCenter.DaXMode.MessageEvent += new DaInterFace.MessageHandler(XMode_MessageEvent);
            dataCenter.DaPDA.MessageEvent += new PDAMessageHandler(PDA_MessageEvent);
            //dataCenter.DaPDA.ClientMessage.MessageChanged += new MobileClientMessageChange(ClientMessage_MessageChanged);

            dataCenter.DaSYN.MessageEvent += new SYNMessageHandler(SYN_MessageEvent);
            if (dataCenter.DaUMode != null)
            {
                dataCenter.DaUMode.MessageEvent += new MessageHandler(UMode_MessageEvent);
                dataCenter.DaUMode.AddHoleEvent += new AddUModeHoleHandler(UMode_AddHoleEvent);
                dataCenter.DaUMode.AddGateEvent += new AddUModeGateHandler(UMode_AddGateEvent);
                dataCenter.DaUMode.ClearEvent += new ClearUModeItemHandler(UMode_ClearEvent);
            }
            dataCenter.Init();

            Page01_MobilePort.Text = dataCenter.DaConfig.Get("Page01_Port");
            Page01_MobileVistSpan.Value = Decimal.Parse(dataCenter.DaConfig.Get("Page01_Vist"));
            Page02_MobilePort.Text = dataCenter.DaConfig.Get("Page02_Port");
            Page03_Port.Text = dataCenter.DaConfig.Get("Page03_Port");
            Page05_Enable.Checked = Common.CBool(dataCenter.DaConfig.Get("Page05_Enable"));

            Dictionary<String, SetOfBook> books = dataCenter.DaData.GetAccounts();
            foreach (SetOfBook book in books.Values)
            {
                Page01_MobileAccountList.Items.Add(book.FNumber + "|" + book.FName);
            }
            if (Page01_MobileAccountList.Items.Count > 0)
            { 
                
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //this.Hide();

            //this.dataCenter.DaData.GetMeterInfoByCardSerial("1");

            TxtFingerPrint.Text = FingerPrint.Value();
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            //this.Hide();
        }

        private void NotifyMain_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
        }

        private void NotifyStripMenuExit_Click(object sender, EventArgs e)
        {
            allowClose = true;
            this.Close();
            Application.Exit();
        }
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!allowClose)
            {
                e.Cancel = true;
                this.Hide();
            }
        }

        #endregion

        #region Page00 Data-------------------------------------------------------------------------

        private void DataBase_MessageEvent(MessageType type, String note, Double span)
        {
            if (Page00_StopMessage.Checked) return;
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new DataBaseMessageEvent(DataBase_MessageEvent), new object[] { type, note, span });
                return;
            }
            try
            {
                
                ListViewItem lvi = new ListViewItem(DateTime.Now.ToString("yy-MM-dd HH:mm:ss"));
                ListViewItem.ListViewSubItem sbnote = new ListViewItem.ListViewSubItem(lvi, note);
                lvi.SubItems.Add(sbnote);
                if (span > 0)
                {
                    ListViewItem.ListViewSubItem sbtime = new ListViewItem.ListViewSubItem(lvi, span.ToString("0.0") + "ms");
                    lvi.SubItems.Add(sbtime);
                }
                Page00_ListView.Items.Insert(0, lvi);
                //Page00_ListView.Items.Add(lvi);
                switch (type)
                {
                    case MessageType.Note:
                        lvi.ForeColor = Color.White;
                        break;
                    case MessageType.Error:
                        lvi.ForeColor = Color.Red;
                        break;
                    case MessageType.Warning:
                        lvi.ForeColor = Color.Yellow;
                        break;
                }

                if (Page00_ListView.Items.Count > MAX_LINE)
                {
                    Page00_ListView.Items.RemoveAt(Page00_ListView.Items.Count - 1);
                    //Page00_ListView.Items.RemoveAt(0);
                }
            }
            catch (Exception)
            {
            }
        }
        private void Page00_ButtonHide_Click(object sender, EventArgs e)
        {
            Page00_TxtMessage.Text = "";
            Page00_PanelHide.Visible = false;
        }

        private void Page00_ListView_DoubleClick(object sender, EventArgs e)
        {
            if (Page00_ListView.SelectedItems.Count > 0)
            {
                Page00_TxtMessage.Text = Page00_ListView.SelectedItems[0].SubItems[1].Text;
                Page00_PanelHide.Visible = true;
            }
        }

        private void Page00_DataTest_Click(object sender, EventArgs e)
        {
            dataCenter.DaData.Test();
        }

        #endregion

        #region Page01_Mobile-------------------------------------------------------------------------

        private void Mobile_MessageEvent(MessageType type, String note, Double span)
        {
            if (Page01_StopMessage.Checked) return;
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new MobileMessageEvent(Mobile_MessageEvent), new object[] { type, note, span });
                return;
            }
            ListViewItem lvi = new ListViewItem(DateTime.Now.ToString("yy-MM-dd HH:mm:ss"));
            ListViewItem.ListViewSubItem sbnote = new ListViewItem.ListViewSubItem(lvi, note);
            lvi.SubItems.Add(sbnote);
            if (span > 0)
            {
                ListViewItem.ListViewSubItem sbtime = new ListViewItem.ListViewSubItem(lvi, span.ToString("0.0") + "ms");
                lvi.SubItems.Add(sbtime);
            }
            Page01_ListView.Items.Insert(0, lvi);
            switch (type)
            {
                case MessageType.Note:
                    lvi.ForeColor = Color.White;//Color.Lime;
                    break;
                case MessageType.Error:
                    lvi.ForeColor = Color.Red;
                    break;
                case MessageType.Warning:
                    lvi.ForeColor = Color.Yellow;
                    break;
            }

            if (Page01_ListView.Items.Count > MAX_LINE)
            {
                Page01_ListView.Items.RemoveAt(Page01_ListView.Items.Count - 1);
            }
        }

        private void ClientMessage_MessageChanged(int msgcount, int usercount)
        {
            Page01_MsgWaitCount.Text = msgcount.ToString();
            Page01_UserWaitCount.Text = usercount.ToString();
        }

        private void Page01_MobilePort_TextChanged(object sender, EventArgs e)
        {
            dataCenter.DaConfig.Set("Page01_Port", Page01_MobilePort.Text);
        }

        private void Page01_MobileVistSpan_ValueChanged(object sender, EventArgs e)
        {
            dataCenter.DaConfig.Set("Page01_Vist", Page01_MobileVistSpan.Value.ToString("#"));
        }

        private void Page01_ListView_DoubleClick(object sender, EventArgs e)
        {
            if (Page01_ListView.SelectedItems.Count > 0)
            {
                Page01_TxtMessage.Text = Page01_ListView.SelectedItems[0].SubItems[1].Text;
                Page01_PanelHide.Visible = true;
            }
        }

        private void Page01_ButtonHide_Click(object sender, EventArgs e)
        {
            Page01_TxtMessage.Text = "";
            Page01_PanelHide.Visible = false;
        }

        private void Page01_MobileMsgTestButton_Click(object sender, EventArgs e)
        {
            if (Page01_MobileMsgTest.Text == "") return;
            List<String> names = new List<string>();
            String account = Page01_MobileAccountList.Text.Split('|')[0];
            String[] namelist = Page01_MobileMsgTest.Text.Replace("\n", "").Split('\r');
            for (int i = 0; i < namelist.Length; i++)
            {
                if (namelist[i] != String.Empty)
                {
                    names.Add(namelist[i]);
                }
            }
            dataCenter.DaMobile.ClientMessage.SendByName(account, names, "系统消息", "测试消息标题", "测试消息内容");

            //MessageBox.Show(dataCenter.DaMobile.Json.ClassToJson(dataCenter.DaMobile.ClientMessage.GetByName("2", "0000")));
        }

        #endregion

        #region Page02_PDA-------------------------------------------------------------------------

        private void PDA_MessageEvent(MessageType type, String note, Double span)
        {
            if (Page02_StopMessage.Checked) return;
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new PDAMessageEvent(PDA_MessageEvent), new object[] { type, note , span});
                return;
            }
            ListViewItem lvi = new ListViewItem(DateTime.Now.ToString("yy-MM-dd HH:mm:ss"));
            ListViewItem.ListViewSubItem sbnote = new ListViewItem.ListViewSubItem(lvi, note);
            lvi.SubItems.Add(sbnote);
            if (span > 0)
            {
                ListViewItem.ListViewSubItem sbtime = new ListViewItem.ListViewSubItem(lvi, span.ToString("0.0") + "ms");
                lvi.SubItems.Add(sbtime);
            }
            Page02_ListView.Items.Insert(0, lvi);
            switch (type)
            {
                case MessageType.Note:
                    lvi.ForeColor = Color.White;//Color.Lime;
                    break;
                case MessageType.Error:
                    lvi.ForeColor = Color.Red;
                    break;
                case MessageType.Warning:
                    lvi.ForeColor = Color.Yellow;
                    break;
            }

            if (Page02_ListView.Items.Count > MAX_LINE)
            {
                Page02_ListView.Items.RemoveAt(Page02_ListView.Items.Count - 1);
            }
        }

        private void Page02_ButtonHide_Click(object sender, EventArgs e)
        {
            Page02_TxtMessage.Text = "";
            Page02_PanelHide.Visible = false;
        }

        private void Page02_MobilePort_TextChanged(object sender, EventArgs e)
        {
            dataCenter.DaConfig.Set("Page02_Port", Page02_MobilePort.Text);
        }

        private void Page02_ListView_DoubleClick(object sender, EventArgs e)
        {
            if (Page02_ListView.SelectedItems.Count > 0)
            {
                Page02_TxtMessage.Text = Page02_ListView.SelectedItems[0].SubItems[1].Text;
                Page02_PanelHide.Visible = true;
            }
        }
        #endregion

        #region Page03_SYN-------------------------------------------------------------------------

        private void SYN_MessageEvent(MessageType type, String note, Double span)
        {
            if (Page03_StopMessage.Checked) return;
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new SYNMessageEvent(SYN_MessageEvent), new object[] { type, note, span });
                return;
            }
            ListViewItem lvi = new ListViewItem(DateTime.Now.ToString("yy-MM-dd HH:mm:ss"));
            ListViewItem.ListViewSubItem sbnote = new ListViewItem.ListViewSubItem(lvi, note);
            lvi.SubItems.Add(sbnote);
            if (span > 0)
            {
                ListViewItem.ListViewSubItem sbtime = new ListViewItem.ListViewSubItem(lvi, span.ToString("0.0") + "ms");
                lvi.SubItems.Add(sbtime);
            }
            Page03_ListView.Items.Insert(0, lvi);
            switch (type)
            {
                case MessageType.Note:
                    lvi.ForeColor = Color.White;//Color.Lime;
                    break;
                case MessageType.Error:
                    lvi.ForeColor = Color.Red;
                    break;
                case MessageType.Warning:
                    lvi.ForeColor = Color.Yellow;
                    break;
            }

            if (Page03_ListView.Items.Count > MAX_LINE)
            {
                Page03_ListView.Items.RemoveAt(Page03_ListView.Items.Count - 1);
            }
        }

        private void Page03_ButtonHide_Click(object sender, EventArgs e)
        {
            Page03_TxtMessage.Text = "";
            Page03_PanelHide.Visible = false;
        }

        private void Page03_MobilePort_TextChanged(object sender, EventArgs e)
        {
            dataCenter.DaConfig.Set("Page03_Port", Page03_Port.Text);
        }

        private void Page03_ListView_DoubleClick(object sender, EventArgs e)
        {
            if (Page03_ListView.SelectedItems.Count > 0)
            {
                Page03_TxtMessage.Text = Page03_ListView.SelectedItems[0].SubItems[1].Text;
                Page03_PanelHide.Visible = true;
            }
        }


        #endregion

        #region Page04_XMode-------------------------------------------------------------------------
        private void XMode_MessageEvent(MessageType type, String note, Double span)
        {
            if (Page04_StopMessage.Checked) return;
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new XModeMessageEvent(XMode_MessageEvent), new object[] { type, note, span });
                return;
            }
            ListViewItem lvi = new ListViewItem(DateTime.Now.ToString("yy-MM-dd HH:mm:ss"));
            ListViewItem.ListViewSubItem sbnote = new ListViewItem.ListViewSubItem(lvi, note);
            lvi.SubItems.Add(sbnote);
            if (span > 0)
            {
                ListViewItem.ListViewSubItem sbtime = new ListViewItem.ListViewSubItem(lvi, span.ToString("0.0") + "ms");
                lvi.SubItems.Add(sbtime);
            }
            Page04_ListView.Items.Insert(0, lvi);
            switch (type)
            {
                case MessageType.Note:
                    lvi.ForeColor = Color.White;//Color.Lime;
                    break;
                case MessageType.Error:
                    lvi.ForeColor = Color.Red;
                    break;
                case MessageType.Warning:
                    lvi.ForeColor = Color.Yellow;
                    break;
            }

            if (Page04_ListView.Items.Count > MAX_LINE)
            {
                Page04_ListView.Items.RemoveAt(Page03_ListView.Items.Count - 1);
            }
        }

        private void Page04_ButtonHide_Click(object sender, EventArgs e)
        {
            Page04_TxtMessage.Text = "";
            Page04_PanelHide.Visible = false;
        }

        private void Page04_ListView_DoubleClick(object sender, EventArgs e)
        {
            if (Page04_ListView.SelectedItems.Count > 0)
            {
                Page04_TxtMessage.Text = Page04_ListView.SelectedItems[0].SubItems[1].Text;
                Page04_PanelHide.Visible = true;
            }
        }
        #endregion

        #region Page05_UMode-------------------------------------------------------------------------

        private void UMode_Load()
        { 
            
        }

        private void UMode_AddHoleEvent(IUModeHole item)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new AddUModeHoleHandler(UMode_AddHoleEvent), new object[] { item });
                return;
            }
            DataApplication.UMode.UModeViewItem viewItem = new UMode.UModeViewItem(item);
            viewItem.BingView(UModeHoleListView);
        }

        private void UMode_AddGateEvent(IUModeGate item)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new AddUModeGateHandler(UMode_AddGateEvent), new object[] { item });
                return;
            }

            DataApplication.UMode.UModeViewItem viewItem = new UMode.UModeViewItem(item);
            viewItem.BingView(UModeHoleListView);
        }

        private void UMode_ClearEvent()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new ClearUModeItemHandler(UMode_ClearEvent), new object[] { });
                return;
            }
            UModeHoleListView.Items.Clear();
        }

        private void UMode_MessageEvent(MessageType type, String note, Double span)
        {
            //if (Page03_StopMessage.Checked) return;
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new DaInterFace.MessageHandler(UMode_MessageEvent), new object[] { type, note, span });
                return;
            }
            ListViewItem lvi = new ListViewItem(DateTime.Now.ToString("yy-MM-dd HH:mm:ss"));
            ListViewItem.ListViewSubItem sbnote = new ListViewItem.ListViewSubItem(lvi, note);
            lvi.SubItems.Add(sbnote);
            Page05_ListView.Items.Insert(0, lvi);
            switch (type)
            {
                case MessageType.Note:
                    lvi.ForeColor = Color.White;//Color.Lime;
                    break;
                case MessageType.Error:
                    lvi.ForeColor = Color.Red;
                    break;
                case MessageType.Warning:
                    lvi.ForeColor = Color.Yellow;
                    break;
            }

            if (Page05_ListView.Items.Count > MAX_LINE)
            {
                Page05_ListView.Items.RemoveAt(Page05_ListView.Items.Count - 1);
            }
        }

        private void Page05_ButtonHide_Click(object sender, EventArgs e)
        {
            Page05_TxtMessage.Text = "";
            Page05_PanelHide.Visible = false;
        }

        private void Page05_ListView_DoubleClick(object sender, EventArgs e)
        {
            if (Page05_ListView.SelectedItems.Count > 0)
            {
                Page05_TxtMessage.Text = Page05_ListView.SelectedItems[0].SubItems[1].Text;
                Page05_PanelHide.Visible = true;
            }
        }

        private void Page05_Enable_CheckedChanged(object sender, EventArgs e)
        {
            dataCenter.DaConfig.Set("Page05_Enable", Page05_Enable.Checked.ToString());
        }

        private void UMode_ReStart_Click(object sender, EventArgs e)
        {
            dataCenter.DaUMode.Load();
        }

        #endregion

        private void Page04_StopMessage_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void Page03_StopMessage_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void MainTimer_Tick(object sender, EventArgs e)
        {
            if (!isAutoMinForm)
            {
                //this.Hide();
                isAutoMinForm = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FormFrpView form = new FormFrpView(dataCenter);
            form.Show();
        }
    }
}
